import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// A.--- B.---  D.---(.*)D
// A.  B.
// C.  D.  (.*\\n.*)D
//A.   ((.*\n)*)
//B.
//C. ((.*\n)+?)|
//D.

/**
 * @Author: WangYongGan
 * @Date: 2021/11/18 18:19
 * @Description:
 */

public class group {
    static String outPath = "question0.txt";
    static String badPath = "badQuestion0.txt";
    static BufferedWriter bufferedWriter;
    static BufferedWriter badBufferedWriter;//( A B C)
    static String answerRe = "（\\s*[A-E]+\\s*）";
    static Pattern answerPattern = Pattern.compile(answerRe, Pattern.MULTILINE);
    static boolean isGood = true;
    static String content = "";
    static String questionRe = "";
    static String orderRe = "\\d+、";
    static String divLine = "<div-n>";
    static String divQuestion = "<div-q>";
    static List<String> questionRes = new ArrayList<>();
    static String bracketQuestionRe = "(（.+?）)";
    static Pattern bracketPattern = Pattern.compile(bracketQuestionRe, Pattern.MULTILINE);
    static Matcher questionMatch = Pattern.compile(questionRe, Pattern.MULTILINE).matcher(content);

    static {
        try {
//            questionRes.add("(\\d.*A\\..*B.*<n>[0-9]");//D
            List<String> list = Arrays.asList("C", "D", "E");
//            questionRes.add("(\\d.*A\\..+?D\\..+?<div-n>[0-9])");
            questionRes.add("(\\d+.+?A\\..+?<div-n>[0-9])");//普通
            questionRes.add("(\\d+.+?A\\．.+?<div-n>[0-9])");//中文字符．
//            list.forEach(re -> questionRes.add("(\\d.*A\\..*" + re + ".*" + divLine + "[0-9]"));
//            questionRes.add("(\\d+、.*(.*\\n)*?A\\.(.*\\n)*?.*C\\..*\\n)");
            System.setErr(new PrintStream("error.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String readFile(String path) throws IOException {
        String content1 = "";
        String line;
        try (BufferedReader bf = new BufferedReader(new FileReader(path))) {
            while ((line = bf.readLine()) != null)
                content1 += (line) + divLine;
        }
        content1 = content1.replace("\t", " ");
        return content1;
    }

    private static void error(Object... objects) {
        String s = Arrays.toString(objects);
        System.err.println(s.substring(1, s.length() - 1));
    }

    private static boolean isValid(String question) {
        if (question.length() < 15) {
            return false;
        }
        for (int i = 0; i < question.length(); i++) {
            char c = question.charAt(i);
            if (c == '、')
                break;
            if (c < '0' || c > '9')
                return false;
        }
        return true;
    }

    private static String trimSting(String str) {
        String res = "";
        for (int i = 0; i < str.length(); i++)
            if (!Character.isWhitespace(str.charAt(i)))
                res += str.charAt(i);
        return res;
    }

    private static String questionHandler(String question) {
        question = question.substring(0, question.length() - divLine.length() - 1);//多-1去掉数字
        Matcher answerMatcher = answerPattern.matcher(question);
        int start = 0;
        int lastStart = start;
        int n = 0;
        while (start < question.length() && answerMatcher.find(start) && ++n < 3) {
            lastStart = start;
            start = answerMatcher.end();
        }
        if (n == 1) {
            isGood = true;
            return question.substring(0, lastStart) + trimSting(question.substring(lastStart, start)) + "<answer>" + question.substring(start);
        }
        isGood = false;
        return question;
    }

    private static void reByQuestion() throws IOException {
        int start = 0;
        int inx = 0;
        String lastQuestion = "";
        while (start < content.length() && questionMatch.find(start)) {
            String question = questionMatch.group(1);
            if (!isValid(question)) {
                error("index:" + start, lastQuestion);
                System.out.println("错误、lastQuestion:" + start + lastQuestion);
                start += 1;
                continue;
            }
            //括号处理
            question = questionHandler(question);
            question = question.endsWith("\n") ? question : question + "\n";
            if (isGood)
                bufferedWriter.write(question + divQuestion);
            else
                badBufferedWriter.write(question);
            lastQuestion = question;
            start = questionMatch.end() - 1;
            System.out.println(question);
            System.out.println(++inx + "条");
        }
        bufferedWriter.close();
        badBufferedWriter.close();
    }

    public static void convertDiv(String path) {

    }

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < questionRes.size(); i++) {
            outPath = "question" + i + ".txt";
            content = readFile(badPath);
            badPath = "badQuestion" + (i + 1) + ".txt";
            questionRe = questionRes.get(i);
            bufferedWriter = new BufferedWriter(new FileWriter(outPath, false));
            badBufferedWriter = new BufferedWriter(new FileWriter(badPath, false));
            questionMatch = Pattern.compile(questionRe, Pattern.MULTILINE).matcher(content);
            reByQuestion();
        }
    }
}
